<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从流或文件指针中获取 header/meta 数据</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.stream-get-line.html">« stream_get_line</a></li>
      <li style="float: right;"><a href="function.stream-get-transports.html">stream_get_transports »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.stream.html">Stream 函数</a></li>
    <li>从流或文件指针中获取 header/meta 数据</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.stream-get-meta-data" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_get_meta_data</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">stream_get_meta_data</span> &mdash; <span class="dc-title">从流或文件指针中获取 header/meta 数据</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-get-meta-data-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_get_meta_data</strong></span>(<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   返回现有 <code class="parameter">stream</code> 的有关信息。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-get-meta-data-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">
       流可以是通过 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>、<span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span>、
        <span class="function"><a href="function.pfsockopen.html" class="function">pfsockopen()</a></span>、<span class="function"><a href="function.stream-socket-client.html" class="function">stream_socket_client()</a></span>
       创建的任何流。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-get-meta-data-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回的数组包含以下元素：
  </p>
  <ul class="itemizedlist">
   <li class="listitem">
    <p class="para">
     <code class="literal">timed_out</code>（bool）——如果流在最后调用 
     <span class="function"><a href="function.fread.html" class="function">fread()</a></span> 或 <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span> 
     时等待超时，则为 <strong><code>true</code></strong>。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">blocked</code>（bool）——如果流是堵塞 IO 
     模式，则为 <strong><code>true</code></strong>。参见 <span class="function"><a href="function.stream-set-blocking.html" class="function">stream_set_blocking()</a></span>。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">eof</code>（bool）——如果流已经到达文件结尾（end-of-file），则为 <strong><code>true</code></strong>。
     注意套接字流即使 <code class="literal">unread_bytes</code> 
     不为零也可以为 <strong><code>true</code></strong>。确定是否还可以读取更多数据，使用 
     <span class="function"><a href="function.feof.html" class="function">feof()</a></span> 而不是读取此值。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">unread_bytes</code>（int）——当前在 PHP 内部缓冲区的字节数。
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <span class="simpara">
      不要在脚本中使用此值。
     </span>
    </p></blockquote>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">stream_type</code>（string）——描述了流底层实现的标记。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">wrapper_type</code>（string）——描述了在流之上的协议封装实现的标记。更多关于封装协议的信息见
    <a href="wrappers.html" class="xref">支持的协议和封装协议</a>。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">wrapper_data</code>（mixed）——当前流附加的封装协议数据。更多封装协议及其数据的信息见
      <a href="wrappers.html" class="xref">支持的协议和封装协议</a>。
    </p>
   </li>
   
   <li class="listitem">
    <p class="para">
     <code class="literal">mode</code>（string）——对当前流所要求的访问类型（见
     <a href="function.fopen.html" class="link">fopen()</a> 参考中的表格 1）。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">seekable</code>（bool）——是否可以在当前流中定位。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">uri</code>（string）——与当前流关联的 URI 或文件名。
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <code class="literal">crypto</code>（array）——当前流的 TLS 
     连接元数据。（注意：仅在资源流使用 TLS 时提供。）
    </p>
   </li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-get-meta-data-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3769">
    <p><strong>示例 #1 <span class="function"><strong>stream_get_meta_data()</strong></span> 示例，<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 和 http 一起使用</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">;<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;open&nbsp;URL&nbsp;(</span><span style="color: #0000BB">$url</span><span style="color: #DD0000">)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$meta&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$meta</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(10) {
  &#039;timed_out&#039; =&gt;
  bool(false)
  &#039;blocked&#039; =&gt;
  bool(true)
  &#039;eof&#039; =&gt;
  bool(false)
  &#039;wrapper_data&#039; =&gt;
  array(13) {
    [0] =&gt;
    string(15) &quot;HTTP/1.1 200 OK&quot;
    [1] =&gt;
    string(11) &quot;Age: 244629&quot;
    [2] =&gt;
    string(29) &quot;Cache-Control: max-age=604800&quot;
    [3] =&gt;
    string(38) &quot;Content-Type: text/html; charset=UTF-8&quot;
    [4] =&gt;
    string(35) &quot;Date: Sat, 20 Nov 2021 18:17:57 GMT&quot;
    [5] =&gt;
    string(24) &quot;Etag: &quot;3147526947+ident&quot;&quot;
    [6] =&gt;
    string(38) &quot;Expires: Sat, 27 Nov 2021 18:17:57 GMT&quot;
    [7] =&gt;
    string(44) &quot;Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT&quot;
    [8] =&gt;
    string(22) &quot;Server: ECS (chb/0286)&quot;
    [9] =&gt;
    string(21) &quot;Vary: Accept-Encoding&quot;
    [10] =&gt;
    string(12) &quot;X-Cache: HIT&quot;
    [11] =&gt;
    string(20) &quot;Content-Length: 1256&quot;
    [12] =&gt;
    string(17) &quot;Connection: close&quot;
  }
  &#039;wrapper_type&#039; =&gt;
  string(4) &quot;http&quot;
  &#039;stream_type&#039; =&gt;
  string(14) &quot;tcp_socket/ssl&quot;
  &#039;mode&#039; =&gt;
  string(1) &quot;r&quot;
  &#039;unread_bytes&#039; =&gt;
  int(1256)
  &#039;seekable&#039; =&gt;
  bool(false)
  &#039;uri&#039; =&gt;
  string(23) &quot;http://www.example.com/&quot;
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3770">
    <p><strong>示例 #2 <span class="function"><strong>stream_get_meta_data()</strong></span> 示例，<span class="function"><a href="function.stream-socket-client.html" class="function">stream_socket_client()</a></span> 和 https 一起使用</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$streamContext&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ssl'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'capture_peer_cert'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'capture_peer_cert_chain'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'disable_compression'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;]<br />);<br /><br /></span><span style="color: #0000BB">$client&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ssl://www.example.com:443'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$errorNumber</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$errorDescription</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">40</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">STREAM_CLIENT_CONNECT</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$streamContext<br /></span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #0000BB">$meta&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$client</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$meta</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
array(8) {
  &#039;crypto&#039; =&gt;
  array(4) {
    &#039;protocol&#039; =&gt;
    string(7) &quot;TLSv1.3&quot;
    &#039;cipher_name&#039; =&gt;
    string(22) &quot;TLS_AES_256_GCM_SHA384&quot;
    &#039;cipher_bits&#039; =&gt;
    int(256)
    &#039;cipher_version&#039; =&gt;
    string(7) &quot;TLSv1.3&quot;
  }
  &#039;timed_out&#039; =&gt;
  bool(false)
  &#039;blocked&#039; =&gt;
  bool(true)
  &#039;eof&#039; =&gt;
  bool(false)
  &#039;stream_type&#039; =&gt;
  string(14) &quot;tcp_socket/ssl&quot;
  &#039;mode&#039; =&gt;
  string(2) &quot;r+&quot;
  &#039;unread_bytes&#039; =&gt;
  int(0)
  &#039;seekable&#039; =&gt;
  bool(false)
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-get-meta-data-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
     本函数对通过 <a href="ref.sockets.html" class="link">Socket 扩展</a>创建的套接字无效。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-get-meta-data-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.get-headers.html" class="function" rel="rdfs-seeAlso">get_headers()</a> - 取得服务器响应 HTTP 请求所发送的所有标头</span></li>
    <li class="member"><a href="reserved.variables.httpresponseheader.html" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>